package com.huifaapp.mall.activity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import com.blankj.utilcode.util.SPUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.huifaapp.mall.R;
import com.huifaapp.mall.afinal.SPConstants;
import com.huifaapp.mall.api.ApiManager;
import com.huifaapp.mall.uitils.AUniqueIdentifier;
import com.squareup.okhttp.Request;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import org.json.JSONException;
import org.json.JSONObject;

public class PetnameActivity extends AppCompatActivity {

    private EditText Ednameset;
    private Button Btsaveset;
    private SharedPreferences.Editor edit;
    private SharedPreferences sharedPreferences;
    private ImageView Imback;
    private Intent intent;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_petname);
        sharedPreferences = getSharedPreferences("name", MODE_APPEND);
        edit = sharedPreferences.edit();

        initView();


    }

    private void initView() {

        Imback = (ImageView) findViewById(R.id.iv_nameset_back);
        Ednameset = (EditText) findViewById(R.id.textnameset);
        Btsaveset = (Button) findViewById(R.id.buttonsaveset);
        Btsaveset.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                final String text = Ednameset.getText().toString();
                if (text.isEmpty()) {
                    Toast.makeText(PetnameActivity.this, "用户昵称不能为空", Toast.LENGTH_SHORT).show();
                } else {
                    new Handler().post(new Runnable() {
                        @Override
                        public void run() {
                            OkHttpUtils.post()
//                                    .url("http://develop.jhjvip.cn:8080/mall/mobile/users/manageuserNickname?userNickname=jjj&token=201712291124171334f3ed61d93e05404b8dc78f10c4770cf8&appId=D7B72681-8205-4C8F-8CCB-142C448203E3")
                                    .url(ApiManager.BASE_NIUPAI_URL+"users/manageuserNickname")
                                    .addParams("userNickname",text)
                                    .addParams("token", SPUtils.getInstance().getString(SPConstants.KEY_USER_TOKEN))
                                    .addParams("appId", AUniqueIdentifier.getDeviceId(PetnameActivity.this))
                                    .build()
                                    .execute(new StringCallback() {
                                        @Override
                                        public void onError(Request request, Exception e) {

                                        }

                                        @Override
                                        public void onResponse(String response) {
                                            Log.e("HB",response);
                                            try {
                                                JSONObject jsonObject = new JSONObject(response);
                                                String code = jsonObject.getString("code");
                                                if (code.equals("10000")){ //请求成功！
                                                    intent=new Intent();
                                                    intent.putExtra("name",text);
                                                    setResult(2222, intent);
                                                    finish();
                                                }else {
                                                    ToastUtils.showShort("保存失败，请稍后再试...");
                                                }
                                            } catch (JSONException e) {
                                                e.printStackTrace();
                                            }
                                        }
                                    });
                        }
                    });
                }
            }
        });
        Imback.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
    }


}
